PROC IMPORT datafile="C:\Sasha\Idiosyncratic Volatility\2018.xls"
out=avnews18 dbms=excel replace; range="volshocks$";
data sasuser.avnews18; set avnews18; if trmivol=-99 then trmivol=.;
if trmivol5=-99 then trmivol5=.; run;
rsubmit;
proc upload data=sasuser.avnews18 out=temp.avfactor (replace=yes);
data index; set ff.factors_monthly (keep = date mktrf rf smb hml);
yearm=(year(date)-1963)*12+month(date); mkt=mktrf*100; rf0=rf*100;
smb0=smb*100; hml0=hml*100; drop date mktrf rf smb hml;
data index; set index; rf=rf0; smb=smb0; hml=hml0;
drop rf0 smb0 hml0; proc sort; by yearm;
data avfactor; set temp.avfactor; yearm=(year-1963)*12+month;
drop year month; proc sort; by yearm;
data factor; merge index avfactor (in=in1);
by yearm; if in1; drop date av; proc means; run;

rsubmit;
data delist; set crsp.mse (keep = permno date event dlret dlstcd);
where event="DELIST" and dlret<10 and 1957<year(date)<2018;
year=year(date); month=month(date); drop date; proc sort; by permno year month;
data crsp; set crsp.msf (keep = permno cusip shrout hsiccd date ret prc vol);
where 1957<year(date)<2018; year=year(date); month=month(date); tvol=abs(prc)*vol;
quar=qtr(date); price=abs(prc); me=abs(prc)*shrout;
proc sort; by permno year month; data crsp; merge crsp delist;
by permno year month; drop date prc vol; proc means; run;

data exch; set crsp.mse (keep = date permno exchcd shrcd);
year=year(date); month=month(date); drop date;
proc sort data=exch; by permno year month;
data crsp; merge crsp exch; by permno year month;
data temp.crsp; set crsp; by permno year month;
i=0; do while(i<2); lexchcd = lag(exchcd); lshrcd=lag(shrcd);
if permno = lag(permno) and exchcd = . then exchcd = lexchcd;
if permno = lag(permno) and shrcd = . then shrcd = lshrcd;
i+1; end; if last.month then output; drop i lexchcd lshrcd;
proc sort data=temp.crsp nodupkey; by permno year month;
data temp.crsp; set temp.crsp;
if (nmiss(dlret)=1 or dlret=0) and 500<=dlstcd<600 and (exchcd ne 0 
and exchcd ne 3 and exchcd ne 33) then dlret=-0.3;
if (nmiss(dlret)=1 or dlret=0) and 500<=dlstcd<600 and
(exchcd=0 or exchcd=3 or exchcd=33) then dlret=-0.55;
if nmiss(ret)=1 and nmiss(dlret)=0 then return=dlret*100;
else return=ret*100; if shrcd ne 10 and shrcd ne 11 then delete;
if (exchcd=3 or exchcd=33) and year<2001 then tvol=tvol/2;
if (exchcd=3 or exchcd=33) and (year=2001 and month=1) then tvol=tvol/2;
if (exchcd=3 or exchcd=33) and (year=2001 and month>1) then tvol=tvol/1.8;
if (exchcd=3 or exchcd=33) and (year=2002 or year=2003) then tvol=tvol/1.6;
turn=tvol/me/10; drop event ret dlret dlstcd shrcd hsiccd; proc means; run;

rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
yearm=(year-1963)*12+month; drop year month; proc sort; by yearm;
data crsp; merge crsp factor; by yearm; eret=((1+return/100)/(1+rf/100)-1)*100;
drop return rf trmivol trmivol5 dmavvar11 dmavvar12 dmivol12 dmtvol11 dmtvol12;
if yearm<0 then delete; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=61; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-59)
and yearm le %eval(&myear); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.smb, sub.hml, sub.dmivol11
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=est noprint;
model eret = mkt smb hml dmivol11 / edf; by cusip;
data temp.ivol&myear; set est (keep = cusip dmivol11 _p_ _edf_);
where _p_+_edf_ ge 36; yearm = %eval(&myear); drop _p_ _edf_;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=61; %let endyear=660;
%macro makebeta(st); data temp.dmivol11; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.dmivol11; set temp.dmivol11;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data avsens; set temp.dmivol11; proc sort; by year month;
proc univariate data=avsens noprint; var dmivol11; by year month;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA avsens; MERGE avsens decile; BY year month;
  pdecile=1;
  IF dmivol11 > DEC20 THEN pdecile=2;
  IF dmivol11 > DEC40 THEN pdecile=3;
  IF dmivol11 > DEC60 THEN pdecile=4;
  IF dmivol11 > DEC80 THEN pdecile=5;
  if nmiss(dmivol11) then pdecile=.;
drop dec20 dec40 dec60 dec80; proc sort; by cusip year month;
data crsp; set temp.crsp (keep = cusip year month return me);
where year>1967 and year<2018; proc sort; by cusip year month;
data avsens; merge avsens crsp; by cusip year month;
data avsens; set avsens; lme=lag(me); lcusip=lag(cusip); avdecile=lag(pdecile);
if lcusip ne cusip then avdecile=.; if lcusip ne cusip then lme=.;
proc tabulate data=avsens format=5.3;
class avdecile; var return;
table return, (avdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month; weight lme;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmivol11vw"; RUN;
rsubmit;
options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmivol11ew"; RUN;

rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
yearm=(year-1963)*12+month; drop year month; proc sort; by yearm;
data crsp; merge crsp factor; by yearm; eret=((1+return/100)/(1+rf/100)-1)*100;
drop return rf trmivol dmavvar11 dmavvar12 dmivol11 dmivol12 dmtvol11 dmtvol12;
if yearm<61 then delete; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=121; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-59)
and yearm le %eval(&myear); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.smb, sub.hml, sub.trmivol5
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=est noprint;
model eret = mkt smb hml trmivol5 / edf; by cusip;
data temp.ivol&myear; set est (keep = cusip trmivol5 _p_ _edf_);
where _p_+_edf_ ge 36; yearm = %eval(&myear); drop _p_ _edf_;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=121; %let endyear=660;
%macro makebeta(st); data temp.trmivol5; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.trmivol5; set temp.trmivol5;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data avsens; set temp.trmivol5; proc sort; by year month;
proc univariate data=avsens noprint; var trmivol5; by year month;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA avsens; MERGE avsens decile; BY year month;
  pdecile=1;
  IF trmivol5 > DEC20 THEN pdecile=2;
  IF trmivol5 > DEC40 THEN pdecile=3;
  IF trmivol5 > DEC60 THEN pdecile=4;
  IF trmivol5 > DEC80 THEN pdecile=5;
  if nmiss(trmivol5) then pdecile=.;
drop dec20 dec40 dec60 dec80; proc sort; by cusip year month;
data crsp; set temp.crsp (keep = cusip year month return me);
where year>1967 and year<2018; proc sort; by cusip year month;
data avsens; merge avsens crsp; by cusip year month;
data avsens; set avsens; lme=lag(me); lcusip=lag(cusip); avdecile=lag(pdecile);
if lcusip ne cusip then avdecile=.; if lcusip ne cusip then lme=.;
proc tabulate data=avsens format=5.3;
class avdecile; var return;
table return, (avdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month; weight lme;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="trmivol5vw"; RUN;
rsubmit;
options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="trmivol5ew"; RUN;

rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
yearm=(year-1963)*12+month; drop year month; proc sort; by yearm;
data crsp; merge crsp factor; by yearm; eret=((1+return/100)/(1+rf/100)-1)*100;
drop return rf trmivol trmivol5 dmavvar12 dmivol11 dmivol12 dmtvol11 dmtvol12;
if yearm<0 then delete; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=61; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-59)
and yearm le %eval(&myear); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.smb, sub.hml, sub.dmavvar11
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=est noprint;
model eret = mkt smb hml dmavvar11 / edf; by cusip;
data temp.ivol&myear; set est (keep = cusip dmavvar11 _p_ _edf_);
where _p_+_edf_ ge 36; yearm = %eval(&myear); drop _p_ _edf_;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=61; %let endyear=660;
%macro makebeta(st); data temp.dmavvar11; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.dmavvar11; set temp.dmavvar11;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data avsens; set temp.dmavvar11; proc sort; by year month;
proc univariate data=avsens noprint; var dmavvar11; by year month;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA avsens; MERGE avsens decile; BY year month;
  pdecile=1;
  IF dmavvar11 > DEC20 THEN pdecile=2;
  IF dmavvar11 > DEC40 THEN pdecile=3;
  IF dmavvar11 > DEC60 THEN pdecile=4;
  IF dmavvar11 > DEC80 THEN pdecile=5;
  if nmiss(dmavvar11) then pdecile=.;
drop dec20 dec40 dec60 dec80; proc sort; by cusip year month;
data crsp; set temp.crsp (keep = cusip year month return me);
where year>1967 and year<2018; proc sort; by cusip year month;
data avsens; merge avsens crsp; by cusip year month;
data avsens; set avsens; lme=lag(me); lcusip=lag(cusip); avdecile=lag(pdecile);
if lcusip ne cusip then avdecile=.; if lcusip ne cusip then lme=.;
proc tabulate data=avsens format=5.3;
class avdecile; var return;
table return, (avdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month; weight lme;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmavvar11vw"; RUN;
rsubmit;
options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmavvar11ew"; RUN;

rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
yearm=(year-1963)*12+month; drop year month; proc sort; by yearm;
data crsp; merge crsp factor; by yearm; eret=((1+return/100)/(1+rf/100)-1)*100;
drop return rf trmivol trmivol5 dmavvar11 dmivol11 dmivol12 dmtvol11 dmtvol12;
if yearm<0 then delete; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=61; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-59)
and yearm le %eval(&myear); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.smb, sub.hml, sub.dmavvar12
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=est noprint;
model eret = mkt smb hml dmavvar12 / edf; by cusip;
data temp.ivol&myear; set est (keep = cusip dmavvar12 _p_ _edf_);
where _p_+_edf_ ge 36; yearm = %eval(&myear); drop _p_ _edf_;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=61; %let endyear=660;
%macro makebeta(st); data temp.dmavvar12; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.dmavvar12; set temp.dmavvar12;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data avsens; set temp.dmavvar12; proc sort; by year month;
proc univariate data=avsens noprint; var dmavvar12; by year month;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA avsens; MERGE avsens decile; BY year month;
  pdecile=1;
  IF dmavvar12 > DEC20 THEN pdecile=2;
  IF dmavvar12 > DEC40 THEN pdecile=3;
  IF dmavvar12 > DEC60 THEN pdecile=4;
  IF dmavvar12 > DEC80 THEN pdecile=5;
  if nmiss(dmavvar12) then pdecile=.;
drop dec20 dec40 dec60 dec80; proc sort; by cusip year month;
data crsp; set temp.crsp (keep = cusip year month return me);
where year>1967 and year<2018; proc sort; by cusip year month;
data avsens; merge avsens crsp; by cusip year month;
data avsens; set avsens; lme=lag(me); lcusip=lag(cusip); avdecile=lag(pdecile);
if lcusip ne cusip then avdecile=.; if lcusip ne cusip then lme=.;
proc tabulate data=avsens format=5.3;
class avdecile; var return;
table return, (avdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month; weight lme;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmavvar12vw"; RUN;
rsubmit;
options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmavvar12ew"; RUN;


rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
yearm=(year-1963)*12+month; drop year month; proc sort; by yearm;
data crsp; merge crsp factor; by yearm; eret=((1+return/100)/(1+rf/100)-1)*100;
drop return rf trmivol trmivol5 dmavvar11 dmavvar12 dmivol11 dmtvol11 dmtvol12;
if yearm<0 then delete; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=61; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-59)
and yearm le %eval(&myear); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.smb, sub.hml, sub.dmivol12
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=est noprint;
model eret = mkt smb hml dmivol12 / edf; by cusip;
data temp.ivol&myear; set est (keep = cusip dmivol12 _p_ _edf_);
where _p_+_edf_ ge 36; yearm = %eval(&myear); drop _p_ _edf_;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=61; %let endyear=660;
%macro makebeta(st); data temp.dmivol12; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.dmivol12; set temp.dmivol12;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data avsens; set temp.dmivol12; proc sort; by year month;
proc univariate data=avsens noprint; var dmivol12; by year month;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA avsens; MERGE avsens decile; BY year month;
  pdecile=1;
  IF dmivol12 > DEC20 THEN pdecile=2;
  IF dmivol12 > DEC40 THEN pdecile=3;
  IF dmivol12 > DEC60 THEN pdecile=4;
  IF dmivol12 > DEC80 THEN pdecile=5;
  if nmiss(dmivol12) then pdecile=.;
drop dec20 dec40 dec60 dec80; proc sort; by cusip year month;
data crsp; set temp.crsp (keep = cusip year month return me);
where year>1967 and year<2018; proc sort; by cusip year month;
data avsens; merge avsens crsp; by cusip year month;
data avsens; set avsens; lme=lag(me); lcusip=lag(cusip); avdecile=lag(pdecile);
if lcusip ne cusip then avdecile=.; if lcusip ne cusip then lme=.;
proc tabulate data=avsens format=5.3;
class avdecile; var return;
table return, (avdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month; weight lme;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmivol12vw"; RUN;
rsubmit;
options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmivol12ew"; RUN;

rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
yearm=(year-1963)*12+month; drop year month; proc sort; by yearm;
data crsp; merge crsp factor; by yearm; eret=((1+return/100)/(1+rf/100)-1)*100;
drop return rf trmivol trmivol5 dmavvar11 dmavvar12 dmivol11 dmivol12 dmtvol12;
if yearm<0 then delete; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=61; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-59)
and yearm le %eval(&myear); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.smb, sub.hml, sub.dmtvol11
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=est noprint;
model eret = mkt smb hml dmtvol11 / edf; by cusip;
data temp.ivol&myear; set est (keep = cusip dmtvol11 _p_ _edf_);
where _p_+_edf_ ge 36; yearm = %eval(&myear); drop _p_ _edf_;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=61; %let endyear=660;
%macro makebeta(st); data temp.dmtvol11; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.dmtvol11; set temp.dmtvol11;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data avsens; set temp.dmtvol11; proc sort; by year month;
proc univariate data=avsens noprint; var dmtvol11; by year month;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA avsens; MERGE avsens decile; BY year month;
  pdecile=1;
  IF dmtvol11 > DEC20 THEN pdecile=2;
  IF dmtvol11 > DEC40 THEN pdecile=3;
  IF dmtvol11 > DEC60 THEN pdecile=4;
  IF dmtvol11 > DEC80 THEN pdecile=5;
  if nmiss(dmtvol11) then pdecile=.;
drop dec20 dec40 dec60 dec80; proc sort; by cusip year month;
data crsp; set temp.crsp (keep = cusip year month return me);
where year>1967 and year<2018; proc sort; by cusip year month;
data avsens; merge avsens crsp; by cusip year month;
data avsens; set avsens; lme=lag(me); lcusip=lag(cusip); avdecile=lag(pdecile);
if lcusip ne cusip then avdecile=.; if lcusip ne cusip then lme=.;
proc tabulate data=avsens format=5.3;
class avdecile; var return;
table return, (avdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month; weight lme;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmtvol11vw"; RUN;
rsubmit;
options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmtvol11ew"; RUN;

rsubmit;
data crsp; set temp.crsp (keep = cusip year month return);
yearm=(year-1963)*12+month; drop year month; proc sort; by yearm;
data crsp; merge crsp factor; by yearm; eret=((1+return/100)/(1+rf/100)-1)*100;
drop return rf trmivol trmivol5 dmavvar11 dmavvar12 dmivol11 dmivol12 dmtvol11;
if yearm<0 then delete; proc means; run;

rsubmit;
options nosource nonotes errors=0;
%let begyear=61; %let endyear=660;
%macro makebeta(st);  
%do myear=%eval(&begyear) %to %eval(&endyear);;
data perms; set crsp (keep=cusip yearm);
if yearm eq %eval(&myear); proc sort nodupkey; by cusip;
data sub; set crsp; if yearm ge %eval(&myear-59)
and yearm le %eval(&myear); proc sort; by cusip;
proc sql; create table regdata as select
sub.cusip, sub.eret, sub.mkt, sub.smb, sub.hml, sub.dmtvol12
from perms, work.sub where perms.cusip = sub.cusip; quit;
proc sort data=regdata; by cusip; 
proc reg data=regdata outest=est noprint;
model eret = mkt smb hml dmtvol12 / edf; by cusip;
data temp.ivol&myear; set est (keep = cusip dmtvol12 _p_ _edf_);
where _p_+_edf_ ge 36; yearm = %eval(&myear); drop _p_ _edf_;
run; %end; %mend; %makebeta(1);

rsubmit;
options source notes errors=5;
%let begyear=61; %let endyear=660;
%macro makebeta(st); data temp.dmtvol12; set 
%do myear=%eval(&begyear) %to %eval(&endyear);
temp.ivol&myear %end;; %mend; %makebeta(1);
data temp.dmtvol12; set temp.dmtvol12;
year=floor((yearm-0.01)/12)+1963;
month=yearm-(year-1963)*12; drop _type_ _freq_ yearm;
proc sort; by cusip year month; proc means; run;

rsubmit;
data avsens; set temp.dmtvol12; proc sort; by year month;
proc univariate data=avsens noprint; var dmtvol12; by year month;
output out=decile pctlpts = 20 to 80 by 20 pctlpre=dec;
DATA avsens; MERGE avsens decile; BY year month;
  pdecile=1;
  IF dmtvol12 > DEC20 THEN pdecile=2;
  IF dmtvol12 > DEC40 THEN pdecile=3;
  IF dmtvol12 > DEC60 THEN pdecile=4;
  IF dmtvol12 > DEC80 THEN pdecile=5;
  if nmiss(dmtvol12) then pdecile=.;
drop dec20 dec40 dec60 dec80; proc sort; by cusip year month;
data crsp; set temp.crsp (keep = cusip year month return me);
where year>1967 and year<2018; proc sort; by cusip year month;
data avsens; merge avsens crsp; by cusip year month;
data avsens; set avsens; lme=lag(me); lcusip=lag(cusip); avdecile=lag(pdecile);
if lcusip ne cusip then avdecile=.; if lcusip ne cusip then lme=.;
proc tabulate data=avsens format=5.3;
class avdecile; var return;
table return, (avdecile all)*mean; run;

options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month; weight lme;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmtvol12vw"; RUN;
rsubmit;
options nosource nonotes errors=0;
proc sort data=avsens; by year month avdecile;
%macro a(many); %do i=1 %to 5;
proc means data=avsens noprint;
var return; by year month;
where avdecile=&i; output out=factor&i
mean(return) = port&i; run;
%end; %mend a; %a(1);
data temp.factor; merge factor1 factor2 factor3 factor4 factor5;
by year month; if nmiss(month) then delete;
if year<1968 or year>2017 then delete; drop _freq_ _type_;
proc sort; by year month; proc means; run;
proc download data=temp.factor
out=sasuser.factor25 (replace=yes); run;
endrsubmit;
PROC EXPORT DATA=SASUSER.FACTOR25
OUTFILE= "C:\Sasha\Idiosyncratic Volatility\2018.xls" 
DBMS=EXCEL REPLACE; SHEET="dmtvol12ew"; RUN;
